﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.DVPlugIn
{
    /// <summary>
    /// 上传数据审核插件程序集
    /// </summary>
    [CPPEI.WebApi.AuthPostAction("DVPLUGIN", "Upload", "1.0")]
    public class UploadAction : BaseAction<UploadParams, UploadResult>
    {
        protected override ActionResult<UploadResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            UploadParams opParams)
        {
            if (opParams.File == null || string.IsNullOrEmpty(opParams.File.FileName))
            {
                return new ActionResult<UploadResult>(false, ErrorCode.NoFile, ErrorCode.String(ErrorCode.NoFile), null);
            }

            if(!opParams.File.FileName.ToUpper().EndsWith(".DLL"))
            {
                return new ActionResult<UploadResult>(false, ErrorCode.InvalidFileType_DLL, ErrorCode.String(ErrorCode.InvalidFileType_DLL), null);
            }

            string binDirectory=  System.Web.HttpContext.Current.Server.MapPath("~/bin/");

            SER.DVPlugInService dtSerService = new SER.DVPlugInService();
            string filePath = string.Format("{0}{1}", binDirectory, opParams.File.FileName);
            FileInfo fileInfo = new FileInfo(filePath);
            if (!fileInfo.Directory.Exists)
            {
                fileInfo.Directory.Create();
            }
            opParams.File.SaveAs(filePath);

            Logger.DebugToTag(Library.LogTAG, string.Format("保存数据审核插件文件:{0}", filePath));

            ServiceResult<MOD.MDVPlugInAssembly> serResult = dtSerService.Parse(File.ReadAllBytes(filePath));
            //fileInfo.Delete();

            if(serResult.IsSuccess)
            {
                UploadResult result = new UploadResult();
                result.Id = serResult.Data.Id;
                result.Assembly = serResult.Data.Assembly;
                result.Version = serResult.Data.Version;
                for (int index = 0; index < serResult.Data.Items.Count; index++)
                {
                    UploadResult.Item item = new UploadResult.Item();
                    item.Id = serResult.Data.Items[index].Id;

                    if (!string.IsNullOrEmpty(serResult.Data.Items[index].Description))
                    {
                        item.Description = serResult.Data.Items[index].Description;
                    }
                    else
                    {
                        item.Description = string.Empty;
                    }

                    if(!string.IsNullOrEmpty(serResult.Data.Items[index].Name))
                    {
                        item.Name = serResult.Data.Items[index].Name;
                    }
                    else
                    {
                        item.Name = string.Empty;
                    }

                    result.Items.Add(item);
                }
                return new ActionResult<UploadResult>(result);
            }
            else
            {
                return new ActionResult<UploadResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class UploadParams
    {
        public System.Web.HttpPostedFile File { get; set; }
    }

    public class UploadResult
    {
        public Guid Id { get; set; }
        public string Assembly { get; set; }

        public string Version { get; set; }

        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }

            public string Name { get; set; }

            public string Description { get; set; }
        }
    }
}
